package com.book.breakage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.breakage.entity.Normal;
import com.book.breakage.mapper.NormalMapper;
import com.book.breakage.service.NormalService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 张高赫
 * @Description:  20173954
 */

@Service
public class NormalServiceImpl extends ServiceImpl<NormalMapper, Normal> implements NormalService {
    @Autowired
    NormalMapper normalMapper;

    @Override
    public List<Normal> normalList(Normal normal) {
        QueryWrapper<Normal> normalQueryWrapper = new QueryWrapper<>();
        return this.normalMapper.selectList(normalQueryWrapper);
    }

    @Override
    public IPage<Normal> normalPage(QueryRequest request, Normal normal) {
        QueryWrapper<Normal> normalQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(normal.getBookname())) {
            normalQueryWrapper.like("bookname", normal.getBookname());
        }
        if (StringUtils.isNotEmpty(normal.getPrincipal())) {
            normalQueryWrapper.like("principal", normal.getPrincipal());
        }
        Page<Normal> normalPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(normalPage, normalQueryWrapper);
    }

    @Override
    public void normalAdd(Normal normal) {
        this.save(normal);
    }

    @Override
    public void normalUpdate(Normal normal) {
        this.saveOrUpdate(normal);
    }

    @Override
    public void normalDelete(String[] breReturn) {
        List<String> list = Arrays.asList(breReturn);
        this.removeByIds(list);
    }

    @Override
    public List<Normal> normalApply(String[] normal) {
        List<String> list = Arrays.asList(normal);
        return (List<Normal>)this.listByIds(list);
    }
}
